capture log close
log using $log/AED_analysis_10_average_marginal_effect text replace

/* ------------------------------------------------------------------------------------ */
/* --- Sensitivity Analysis Table A.11 ------------------------------------------------ */
/* ------------------------------------------------------------------------------------ */ 

clear
clear matrix
clear mata
set maxvar 10000
set matsize 2000
set more off

set trace off
   
#d;
global xvars " c.vac_rate c.aloq   
				c.share_agriculture_avg c.share_industry_avg c.share_service_avg c.wanderungen_avg c.bip_avg 
				iza_alter_kat1 iza_alter_kat2 iza_alter_kat3 iza_alter_kat4 iza_famstd
				i.iza_kind i.iza_abschluss_kat i.iza_ausb_kat iza_sex
				c.iza_last_daily_income i.iza_beruf_kat
				iza_ende_last iza_vztz_last c.iza_zeit_la
				c.iza_sv_tminus1 c.iza_sv_tminus2 c.iza_sv_tminus3
				c.iza_lnwage_tminus1 c.iza_lnwage_tminus2 c.iza_lnwage_tminus3 
				i.year i.entry_m";
				#d cr
	

				
	
		use $data/data_est_border, clear
	
	
	
	
	
	
	keep if eintritt<=200805 & eintritt>=200706
	
	
	
		#d;
		egen month_sv1_24 = rsum(sv_tplus_1 sv_tplus_2 sv_tplus_3 sv_tplus_4 sv_tplus_5 sv_tplus_6 sv_tplus_7 sv_tplus_8 sv_tplus_9 sv_tplus_10 
		sv_tplus_11 sv_tplus_12 sv_tplus_13 sv_tplus_14 sv_tplus_15 sv_tplus_16 sv_tplus_17 sv_tplus_18 sv_tplus_19 sv_tplus_20 sv_tplus_21 sv_tplus_22 sv_tplus_23 sv_tplus_24)
		;
	
		egen earnings1_24 = rsum(daily_income_tplus_1 daily_income_tplus_2 daily_income_tplus_3 daily_income_tplus_4 daily_income_tplus_5
		daily_income_tplus_6 daily_income_tplus_7 daily_income_tplus_8 daily_income_tplus_9 daily_income_tplus_10 daily_income_tplus_11
		daily_income_tplus_12 daily_income_tplus_13 daily_income_tplus_14 daily_income_tplus_15 daily_income_tplus_16 
		daily_income_tplus_17 daily_income_tplus_18 daily_income_tplus_19 daily_income_tplus_20
		daily_income_tplus_21 daily_income_tplus_22 daily_income_tplus_23 daily_income_tplus_24);
		replace earnings1_24 = earnings1_24*30;
		#d cr
	
	#d;
	global outc " month_sv1_24 
	earnings1_24	
	 umzugskosten_24";
	#d cr

	
	encode region_pair, g(region_pair2)
	
	gen intensity_high=0
		forvalues x=1/487 {
			qui sum log_intensity_alt if county_pair_`x'==1
			replace intensity_high=1 if log_intensity_alt>r(mean) & county_pair_`x'==1
			
		}
	
	qui reg log_intensity_alt $xvars i.region_pair2 [pw=1/fweight_reg], cluster(ba)
	predict int_res, xb
	
	sum int_res if intensity_high==1
	gen max_high =r(max)
	gen min_high =r(min)
	
	sum int_res if intensity_high==0
	gen max_low =r(max)
	gen min_low =r(min)
	
	egen pmax = rmin(max_high max_low)
	egen pmin = rmax(min_high min_low)
	
	gen trimmed = (int_res<=pmax & int_res>=pmin)
	drop max_high min_high max_low min_low pmax pmin
	
	
	bys iza_id: egen trimmed_temp=min(trimmed)
	drop trimmed 
	
	rename trimmed_temp trimmed
	
	
	
	
	foreach var in $outc {
		
		qui reg `var' log_intensity_alt $xvars pair_* [pw=1/fweight_reg], cluster(ba)
		matrix b=e(b)'
		
		matrix v=e(V)
		matrix se=vecdiag(cholesky(diag(vecdiag(v[1..1,1..1]))))'
		matrix d0_`var'=b[1..1,1..1],se
		
		
		qui reg `var' (c.log_intensity_alt)##($xvars i.region_pair2) [pw=1/fweight_reg], cluster(ba)
		margins, dydx(log_intensity_alt)
		matrix r=r(table)
		scalar b = r[1,1]
	  	 scalar se = r[2,1]
		 
		 
		 
		 matrix d1_`var' = b,se
			
		qui reg `var' (c.log_intensity_alt)##($xvars i.region_pair2) if  trimmed==1 [pw=1/fweight_reg], cluster(ba)
		margins, dydx(log_intensity_alt)
		matrix r=r(table)
		scalar b = r[1,1]
	  	 scalar se = r[2,1]
		 
		 matrix d2_`var' = b,se	
		
		
		
		
		matrix d_`var'=	d0_`var',d1_`var',d2_`var'
		
	}
	
	




matrix d = d_month_sv1_24\d_earnings1_24\d_umzugskosten_24

matrix rownames d = "Months employed" "Earnings" "Relocation assistance" 

	
	matrix colname d =  "Baseline" "SE" "AME" "SE" "AME trimmed" "SE" 
	mat list d


	


log close
